<template>
  <div id="userLoginView">
    <h2 style="margin-bottom: 16px">用户登录</h2>
    <a-form
      style="max-width: 480px; margin: 0 auto"
      label-align="left"
      auto-label-width
      :model="form"
      @submit="handleSubmit"
    >
      <a-form-item field="userAccount" label="账号">
        <a-input v-model="form.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item field="userPassword" tooltip="密码不少于 8 位" label="密码">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 120px">
          登录
        </a-button>
      </a-form-item>
      <!-- 新增：去注册按钮 -->
      <a-form-item>
        <a-button type="text" @click="goRegister" style="width: 120px">
          没有账号？去注册
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import { UserControllerService, UserLoginRequest } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

/**
 * 表单信息
 */
const form = reactive({
  userAccount: "",
  userPassword: "",
} as UserLoginRequest);

const router = useRouter();
const store = useStore();

/**
 * 提交表单
 * @param data
 */
const handleSubmit = async () => {
  const res = await UserControllerService.userLoginUsingPost(form);
  // 登录成功，跳转到主页
  if (res.code === 0) {
    await store.dispatch("user/getLoginUser");
    router.push({
      path: "/",
      replace: true,
    });
  } else {
    message.error("登陆失败，" + res.message);
  }
};

const goRegister = () => {
  router.push({ path: "/user/register" });
};
</script>
<style>
#userLoginView {
  max-width: 500px;
  margin: 40px auto;
  padding: 40px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 16px rgba(0, 0, 0, 0.1);
}

h2 {
  font-size: 24px;
  font-weight: 500;
  color: #1d2129;
  text-align: center;
  margin-bottom: 24px;
}

.arco-form {
  padding: 0 20px;
}

.arco-form-item {
  margin-bottom: 20px;
}

.arco-form-item-label {
  font-weight: 500;
  color: #4e5969;
}

.arco-input,
.arco-input-password {
  background-color: #f7f8fa;
  border-radius: 4px;
  padding: 12px;
  height: auto;
}

.arco-input:hover,
.arco-input-password:hover {
  background-color: #f2f3f5;
}

.arco-btn {
  width: 100%;
  border-radius: 4px;
  font-weight: 500;
  padding: 12px;
  font-size: 16px;
  transition: all 0.2s;
}

.arco-btn-primary {
  background-color: #165dff;
  box-shadow: 0 2px 4px rgba(22, 93, 255, 0.2);
}

.arco-btn-primary:hover {
  background-color: #1e6bff;
  transform: translateY(-1px);
  box-shadow: 0 4px 8px rgba(22, 93, 255, 0.2);
}

/* 响应式调整 */
@media (max-width: 768px) {
  #userLoginView {
    padding: 30px 20px;
    margin: 20px;
  }

  h2 {
    font-size: 20px;
    margin-bottom: 20px;
  }

  .arco-form {
    padding: 0;
  }
}
</style>
